#define _LED_MODULE_

#include "osObjects.h"

#include "chip.h"

#define LED_NUM 8
#define LED_GPIO_CFG IOCON_MODE_PULLUP | IOCON_FUNC0 | IOCON_GPIO_MODE | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF
const uint8_t  LED_GPIO_PORT[LED_NUM] = { 0,  0,  0,  0,  0,  0,  0,  0};
const uint8_t  LED_GPIO_PIN [LED_NUM] = {15, 19, 21, 22, 25, 26, 29, 30};
const uint8_t  LED_GPIO_ON  [LED_NUM] = { 1,  1,  1,  1,  0,  0,  0,  0};
const uint8_t  LED_GPIO_OFF [LED_NUM] = { 0,  0,  0,  0,  1,  1,  1,  1};

 uint8_t led_init(void);
 void    led_on(uint8_t num);
 void    led_off(uint8_t num);
 void    led_toggle(uint8_t num);

/*****************************************************************************//*!
*
* @brief   led task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void LedCtrTask(void const *argument)
{
    led_init();
    
    for(;;){
        osDelay(500);

        led_toggle(0);
        led_toggle(4);
        led_toggle(4);
        led_toggle(6);
        
        
        osDelay(500);
        led_toggle(1);
        led_toggle(3);
        led_toggle(5);
        led_toggle(7);
    }

}


/*****************************************************************************//*!
*
* @brief   led control init.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t led_init(void)
{	
	uint8_t ret = 0;
	uint8_t i = 0;
	
	for(i=0; i<LED_NUM; i++)
	{
		LPC_GPIO->DIR[LED_GPIO_PORT[i]] |= 1U << LED_GPIO_PIN[i];
		LPC_GPIO->B[LED_GPIO_PORT[i]][LED_GPIO_PIN[i]] = LED_GPIO_OFF[i];
		Chip_IOCON_PinMuxSet(LPC_IOCON, LED_GPIO_PORT[i], LED_GPIO_PIN[i], LED_GPIO_CFG);
	}
	
	ret = 1;
	
	return ret;
}

void led_on(uint8_t num)
{
	LPC_GPIO->B[LED_GPIO_PORT[num]][LED_GPIO_PIN[num]] = LED_GPIO_ON[num];
}

void led_off(uint8_t num)
{
	LPC_GPIO->B[LED_GPIO_PORT[num]][LED_GPIO_PIN[num]] = LED_GPIO_OFF[num];
}

void led_toggle(uint8_t num)
{
	LPC_GPIO->NOT[LED_GPIO_PORT[num]] |= (1 << LED_GPIO_PIN[num]);
}



